#pragma once

#include <system/assert.h>

namespace liba {
namespace lib3d {
namespace particles {

template<typename T>
class RefCountedPtr
{
public:
	RefCountedPtr(T* ptr = 0)
	{ 
		p = ptr ? ptr->retain() : 0;
	}

	RefCountedPtr(const RefCountedPtr& rhs)
	{
		p = rhs.p ? rhs.p->retain() : 0;
	}

	const RefCountedPtr& operator=(const RefCountedPtr& rhs)
	{
		if (&rhs == this)
			return *this;

		if (p)
			p->release();

		p = rhs.p ? rhs.p->retain() : 0;
		return *this;
	}

	T* operator->()
	{
		if (!p)
			LIBA_FAIL("Dereferencing zero ptr");
		
		return p;
	}

	T* get()
	{
		return p;
	}

	~RefCountedPtr()
	{
		if (p)
			p->release();
	}
private:
	T* p;
};

} // namespace particles
} // namespace lib3d
} // namespace liba